package www.lagou.dao;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import www.lagou.entity.Phone;
import www.lagou.utils.DruidUtils;

import java.sql.SQLException;
import java.util.List;

public class PhoneDao {

    /**
     * 需求1:  查询价格高于2000元，生产日期是2019年之前的所有手机
     * @param price
     * @param prodate
     * @return
     * @throws SQLException
     */
    public List<Phone> findPhoneByPrice(double price,String prodate) throws SQLException {

        //1.创建QueryRunner对象 自动模式,传入数据库连接池
        QueryRunner qr = new QueryRunner(DruidUtils.dataSource);

        //2.编写SQL
        String sql = "SELECT * FROM phone WHERE price > ? AND prodate < ?";

        //3.设置占位符参数
        Object[] param = {price,prodate};

        List<Phone> list = qr.query(sql,new BeanListHandler<Phone>(Phone.class),param);

        return list;
    }

    /**
     * 需求2:  查询所有颜色是白色的手机信息
     * @param color
     * @return
     * @throws SQLException
     */
    public List<Phone> findPhoneByColor(String color) throws SQLException {

        //1.创建QueryRunner对象 自动模式,传入数据库连接池
        QueryRunner qr = new QueryRunner(DruidUtils.dataSource);

        //2.编写SQL
        String sql = "SELECT * FROM phone WHERE color = ?";

        //3.设置占位符参数
        Object[] param = {color};

        List<Phone> list = qr.query(sql,new BeanListHandler<Phone>(Phone.class),param);

        return list;
    }
}
